/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 10/06/2004
 * Time: 20:53:17
 */
package com.talios.jira.browser;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class JiraListComboBoxModel extends DefaultComboBoxModel {
    private List resolutionList = new ArrayList();

    public JiraListComboBoxModel(List items) {
        resolutionList = items;
    }

    // implements javax.swing.ListModel
    public int getSize() {
        return resolutionList == null ? 0 : resolutionList.size();
    }

    // implements javax.swing.ListModel
    public Object getElementAt(int index) {
        Hashtable project = (Hashtable) resolutionList.get(index);
        if (project != null) {
            return project.get("name");
        } else {
            return "";
        }
    }

    public int getIndexOfId(Object anObject) {
        String projectId = (String) anObject;

        int index = 0;
        for (Object aResolutionList : resolutionList) {
            Hashtable hashtable = (Hashtable) aResolutionList;
            if (projectId.equals(hashtable.get("id"))) {
                return index;
            }

            index++;
        }

        return -1;
    }

    public int getIndexOfName(Object anObject) {
        String projectName = (String) anObject;

        int index = 0;
        for (Object aResolutionList : resolutionList) {
            Hashtable hashtable = (Hashtable) aResolutionList;
            if (projectName.equals(hashtable.get("name"))) {
                return index;
            }

            index++;
        }

        return -1;
    }

    public String getIdOfName(Object selectedItem) {
        int index = getIndexOfName(selectedItem);
        if (index == -1) {
            return null;
        } else {
            Hashtable hashtable = (Hashtable) resolutionList.get(index);
            return (String) hashtable.get("id");
        }
    }

    public String getKeyOfName(Object selectedItem) {
        int index = getIndexOfName(selectedItem);
        if (index == -1) {
            return null;
        } else {
            Hashtable hashtable = (Hashtable) resolutionList.get(index);
            return (String) hashtable.get("key");
        }
    }

    public String getIdOfSelectedItem() {
        return getIdOfName(getSelectedItem());
    }

    public void reverseItems() {
//        Collections.sort(resolutionList, Collections.reverseOrder() );
    }

}